from mcp.server.fastmcp import FastMCP


mcp = FastMCP(host='0.0.0.0', port=9876)


@mcp.tool()
async def plus(a: int | float, b: int | float):
    """两个数相加"""
    try:
        result = a + b
        return f'计算结果: {result}'
    except Exception as e:
        return f'计算错误: {str(e)}'

@mcp.tool()
async def minus(a: int | float, b: int | float):
    """两个数相减"""
    try:
        result = a - b
        return f'计算结果: {result}'
    except Exception as e:
        return f'计算错误: {str(e)}'


@mcp.tool()
async def multiply(a: int | float, b: int | float):
    """两个数相乘"""
    try:
        result = a * b
        return f'计算结果: {result}'
    except Exception as e:
        return f'计算错误: {str(e)}'


@mcp.tool()
async def divide(a: int | float, b: int | float):
    """两个数相除"""
    try:
        result = a / b
        return f'计算结果: {result}'
    except Exception as e:
        return f'计算错误: {str(e)}'


@mcp.tool()
async def negative(a):
    """求一个数的负值"""
    try:
        result = -a
        return f'计算结果: {result}'
    except Exception as e:
        return f'计算错误: {str(e)}'


@mcp.tool()
async def calculate(expression: str) -> str:
    """
    计算一个简单的数学表达式。
    Args:
        expression: 要计算的数学表达式（如"(1+2)*3/4"）
    Returns:
        str: 计算结果
    """
    try:
        result = eval(expression)
        return f'计算结果: {result}'
    except Exception as e:
        return f'计算错误: {str(e)}'


if __name__ == '__main__':
    mcp.run(transport='streamable-http')

